package com.njegovanobradovic.pages.admin;

import java.util.List;

import javax.inject.Inject;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;

import com.njegovanobradovic.entities.Slide;
import com.njegovanobradovic.so.Delete;
import com.njegovanobradovic.so.Get;
import com.njegovanobradovic.so.GetAll;

public class AllSlides {
	@Inject
	private GetAll<Slide> getAllSlides;
	@Property
	private Slide slide;
	
	public List<Slide> getSlideList(){
		try {
			getAllSlides.setSearchForType(Slide.class);
			return getAllSlides.execute();
		} catch (Exception e) {
			return null;
		}
	}
	
	@Inject
	private Delete<Slide> deleteSlide;
	@CommitAfter
	public void onDelete(Long id) {
		try {
			deleteSlide.setId(id);
			deleteSlide.setSearchForType(Slide.class);
			System.out.println(deleteSlide.execute());
		} catch (Exception e) {
			System.out.println("**********************Error on delete********************\n"+e);
		}
	}
	
	public boolean isFirstSlide(Slide slide){
		if (slide.getOrderNum()==1) {
			return true;
		}
		return false;
	}
	
	@InjectPage
	private EditSlide editSlide;
	@Inject
	private Get<Slide> getSlide;
	public Object onEdit(Long id) {
		try {
			getSlide.setId(id);
			getSlide.setSearchForType(Slide.class);
			editSlide.setSlide(getSlide.execute());
			return editSlide;
		} catch (Exception e) {
			System.out.println("**********************Error on edit********************\n"+e);
			return null;
		}
	}
}
